package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.pojo.MyNode;
import com.hqyj.pojo.UserInfo;
import com.hqyj.dao.UserInfoMapper;
import com.hqyj.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.utile.JwtUtil;
import com.hqyj.utile.MdFiveUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2023-10-07
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {
    @Resource
    UserInfoMapper userInfoMapper;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    //读取锁定次数
    @Value("${pwdLock}")
    Integer num;

    @Override
    public HashMap<String, Object> login(UserInfo userInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义键名，保证唯一性，一个用户对应一个
        String keyLock=userInfo.getUserName()+":num";

        String key=userInfo.getUserName()+":lock";

        //获取当前key的值是否存在，判断账号是否锁定
        Object obj = redisTemplate.opsForValue().get(key);
        if(obj!=null){
            map.put("code",500);
            map.put("msg","你的账号被锁定");
            return map;
        }
        //1 判断用户名是否正确
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userInfo.getUserName());
        UserInfo user = userInfoMapper.selectOne(queryWrapper);
        if(user!=null){
            //加密用户输入的密码
            String pwd = MdFiveUtil.pwdChange(userInfo.getUserPwd(),user.getUserSalt());
            if(pwd.equals(user.getUserPwd())){
                //创建token
                String token = JwtUtil.createUserToken(user.getUserId()+"",user.getUserName(),user.getUserSalt());
                map.put("code",200);
                map.put("msg","登录成功");
                map.put("token",token);


            }else{
                //增加次数
                redisTemplate.opsForValue().increment(keyLock,1);
                //设置keyLock键的失效时间1分钟
                redisTemplate.opsForValue().getOperations().expire(keyLock,1, TimeUnit.MINUTES);
                //取keyLock键的值
                Integer i = Integer.parseInt(redisTemplate.opsForValue().get(keyLock)+"");
                if(i<num){
                    //
                    log.info("你的密码输入错误{},还剩余{}次",i,num-i);
                    map.put("code",500);
                    map.put("msg","你的密码输入错误"+i+"次,还剩余"+(num-i)+"次");
                }else{
                    map.put("code",500);
                    map.put("msg","你的账号被锁定");
                    log.info("输入密码错误次数达到上线，账号被锁定");
                    //锁定2分钟
                    redisTemplate.opsForValue().set(key,33,2,TimeUnit.MINUTES);

                }


            }

        }else{
            map.put("code",500);
            map.put("msg","登录失败");
        }

        return map;
    }

    @Override
    public HashMap<String, Object> queryPowerByUserName(String userName) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<MyNode> list = userInfoMapper.queryPowerByUserName(userName);
        //构建前端菜单数据结构
        List<MyNode> newList = new LinkedList<>();
        if(list.size() > 0){
            for(MyNode n : list){
                if(n.getParentId()==0){
                    //创建父菜单对象
                    MyNode parent = new MyNode();
                    parent.setName(n.getName());
                    parent.setIcon(n.getIcon());
                    parent.setIndex(n.getIndex());
                    //创建子菜单集合
                    List<MyNode> childrenList = new LinkedList<>();
                    //查询父菜单的子菜单
                    for(MyNode son:list){
                        if(son.getParentId()==n.getNodeId()){
                                //构建子菜单对象
                             MyNode s = new MyNode();
                             s.setName(son.getName());
                             s.setUrl(son.getUrl());
                             s.setIcon(son.getIcon());
                            childrenList.add(s);
                        }
                    }
                    parent.setChildren(childrenList);
                    //添加到菜单权限集合中
                    newList.add(parent);

                }

            }

            map.put("code",200);
            map.put("data",newList);
            return map;
        }
        map.put("code",500);
        map.put("data","");
        return map;
    }
}
